package com.panda.qqclient.services;

import com.panda.qqcommon.Message;
import com.panda.qqcommon.MessageType;
import com.panda.qqcommon.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Objects;

public class UserClientService {

/**
 * 用于实现用户的登录和注册验证
 */

private User user=new User();
Socket socket=null;


    //根据username和password到服务器验证是否合法
   public boolean login(String userId, String password)  {
        user.setUserId(userId);
        user.setPassword(password);
        boolean result = false;

       try {
           //连接服务器
           socket = new Socket(InetAddress.getLocalHost(), 9999);
           System.out.println("服务器已经连接");
           //发送User对象
           new ObjectOutputStream(socket.getOutputStream()).writeObject(user);

           //接受User对象
           ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
           Message message = (Message) objectInputStream.readObject();
           System.out.println("接受客户端验证message:"+message);

           if(Objects.equals(message.getMessageType(), MessageType.MESSAGE_LOGIN_SUCCEED)){
               result = true;
               //如果成功登录，就会创建一个线程
               ClientConnectServiceThread clientConnectServiceThread = new ClientConnectServiceThread(socket);
               clientConnectServiceThread.start();
               System.out.println("完成创建客户端线程并启动:"+clientConnectServiceThread.getName());
               //为了后续便于扩展客户端，将线程放到集合中
               ManageClientConnectServerThread.addToMap(userId,clientConnectServiceThread);
           }else {
               //如果连接失败，关闭socket
               socket.close();
                result=false;
           }


       } catch (IOException e) {
           throw new RuntimeException(e);
       } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
       }

       return result;
   }

   public void onlineFriendList() throws IOException {
       Message message = new Message();
       message.setMessageType(MessageType.MESSAGE_GET_ONLINE_LIST);
       message.setSender(user.getUserId());



       //得到该线程的socket
       ClientConnectServiceThread connectServiceThread = ManageClientConnectServerThread.getFromMap(user.getUserId());
       Socket socket = connectServiceThread.getSocket();

       //输出客户请求
       ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
       objectOutputStream.writeObject(message);
   }

   //退出客户端，给服务端发消息
    public void logout() throws IOException {
       Message message = new Message();
       message.setMessageType(MessageType.MESSAGE_CLIENT_EXIT);
       message.setSender(user.getUserId());

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectOutputStream.writeObject(message);
        System.out.println(user.getUserId()+"结束进程");
        System.exit(0);
    }

}
